import { Module } from "vuex";
import { AjaxState, RootState } from "./store.type";
const storeModule: Module<AjaxState, RootState> = {
    namespaced: true,
    state: {
        ajaxLoader: false,
        ajaxLoaderIndex: 0
    },
    actions: {
        ajaxRequestStartAsync({ commit }) {
            commit("ajaxRequestStart");
        },
        ajaxRequestEndAsync({ commit }) {
            setTimeout(() => {
                commit("ajaxRequestEnd");
            }, 300);
        }
    },
    mutations: {
        ajaxRequestStart(state) {
            if (state.ajaxLoaderIndex === 0) state.ajaxLoader = true;
            state.ajaxLoaderIndex++;
        },
        ajaxRequestEnd(state) {
            state.ajaxLoaderIndex--;
            if (state.ajaxLoaderIndex === 0) state.ajaxLoader = false;
        },
        ajaxRequestReset(state) {
            state.ajaxLoaderIndex = 0;
            state.ajaxLoader = false;
        }
    }
};
export default storeModule;
